﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkRecord.CommonClass;

namespace WorkRecord
{
    public partial class Login : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write(CommonClass.Common.GetEncryptString("123456"));
            if (!IsPostBack)
            {
                WebContext.Current.CurrentUser = null;
                HttpContext.Current.Response.Cookies.Clear();
                if (Session["UserAccount"] != null)
                {
                    Txt_UserAccount.Text = HttpUtility.UrlDecode(Session["UserAccount"].ToString());
                }
            }
        }

        protected void btn_Login_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Txt_Password.Text.Trim()))
            {
                ShowAlert("密码不能为空");
                return;
            }


            LoginSys(Txt_UserAccount.Text, Txt_Password.Text);

        }

        private void LoginSys(string useraccount, string password)
        {
            var da = new DataAccess();
            var user =
                da.readdata(
                    string.Format("select * from T_Sys_User where username='{0}' and password='{1}'", useraccount,
                        System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5")));


            if (user != null && user.Rows.Count > 0)
            {

                Session["UserAccount"] = useraccount;


                if (!string.IsNullOrEmpty(Request["ReturnUrl"]))
                {
                    Response.Redirect(Request["ReturnUrl"]);
                }

                //var dt =
                //    da.readdata(
                //        string.Format(
                //            "select * from Journeys where UserAccount='{0}' and CONVERT(varchar(100), SubmitTime, 23)='{1}'",
                //            useraccount, DateTime.Now.ToString("yyyy-MM-dd")));
                //DataTable categoryDt;
                //if ((dt != null && dt.Rows.Count > 0) || WebContext.Current.IsAdministrator || !da.CanEnter(useraccount,out categoryDt))
                //{
                //    Response.Redirect("/WorkView.aspx");
                //}
                //else
                //{
                //    Response.Redirect("/WorkEnter.aspx");
                //}
                Response.Redirect("/WorkView.aspx");
            }
            else
            {
                ShowAlert("用户名或密码错误！");
            }
        }
    }
}